package http;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.Date;

@WebServlet("/cookie")
public class CookieServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");

        response.getWriter().write("<html><body>");
        response.getWriter().write("<ul>");
        for(Cookie cookie : request.getCookies()){
            response.getWriter().append("<li>")
                    .append(cookie.getName())
                    .append(":")
                    .append(cookie.getValue())
                    .append("</li>");
        }
        response.getWriter().write("</ul>");
        response.getWriter().write("</html></body>");

        Cookie  cookie =  new Cookie("time", new Date().toString());
        response.addCookie(cookie);

        cookie =  new Cookie("color", "red");
        response.addCookie(cookie);

        //删除cookie
        cookie = new Cookie("age", "no value");
        cookie.setMaxAge(0);
        response.addCookie(cookie);


    }
}
